package com.lianniu.mgr.projectMgr.projectInfo.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectProcessVo;
import com.lianniu.mgr.projectMgr.projectInfo.service.IProjectProcessService;
import com.lianniu.mgr.util.StringUtil;

@Controller
@RequestMapping("/rest/projectProcess")
public class ProjectProcessRestController {	
	private final static Logger logger = Logger.getLogger(ProjectProcessRestController.class);
	
	@Autowired
	private IProjectProcessService projectProcessService;
		
	/**
	  * @Description:获取进度列表信息
      * @return BaseResult(data=List<ProjectProcessVo>)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/list"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult  list(HttpServletRequest request) {
		logger.info("ProcessController list begin.");
		String projectId = request.getParameter("projectId");
		logger.info("ProcessController list projectId:"+projectId);
		logger.info("processService getProjectProcessesByProjectId begin.");
		BaseResult baseResult = projectProcessService.getProjectProcessesByProjectId(projectId);
		logger.info("processService getProjectProcessesByProjectId end.");
		logger.info("ProcessController list end.");
		return baseResult;
	}
	
	/**
	  * @Description:添加项目进度记录
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/add"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult add(HttpServletRequest request, @RequestParam MultipartFile[] files) throws Exception {
		logger.info("ProcessController add begin.");
		String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" ; 
		List<ProjectProcessVo> list = new ArrayList<>();
		if(files != null && files.length > 0){  
			MultipartFile file;
			File uploadDest = new File(filePath);  
			String[] fileNames = uploadDest.list();  
			String time = "";
			ProjectProcessVo ProjectProcessVo;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String[] key = request.getParameter("key").split(",");
			String[] processDesc = request.getParameter("processDesc").split(",");
			String processId;
			String addUser = request.getParameter("addUser");
			String projectId = request.getParameter("projectId");
			String imgUrl;
			for(int i = 0; i < files.length; i++){
				file = files[i];
				time = "";
				for (int j = 0; j < fileNames.length; j++) {  
					if(file.getOriginalFilename().equals(fileNames[j])){
						time = sdf.format(new Date()) + "-";
						break;
					}
				}  
				imgUrl = filePath + time + file.getOriginalFilename();
				file.transferTo(new File(imgUrl)); 
				// 添加数据数据
				processId = StringUtil.getUUID();
				ProjectProcessVo = new ProjectProcessVo();
				ProjectProcessVo.setId(processId);
				ProjectProcessVo.setProjectId(projectId);
				ProjectProcessVo.setAddUser(addUser);
				ProjectProcessVo.setDelFlag("1");
				if(processDesc.length > i){
					ProjectProcessVo.setProcessDesc(processDesc[i]);
				}
				if(key.length > i){
					ProjectProcessVo.setProcessName(key[i]);
				}
				ProjectProcessVo.setProcessUrl(imgUrl);
				list.add(ProjectProcessVo);
			}
		}
		logger.info("processService insert begin.");
		BaseResult baseResult = projectProcessService.addProjectProcesses(list);
		logger.info("processService insert end.");
		logger.info("ProcessController add end.");
		return baseResult;
	}
	
}
